/*
 * @Author       : wangzeyu 2309335887@qq.com
 * @Date         : 2023-06-23 15:45:16
 * @LastEditors  : WangZeYu
 * @LastEditTime : 2023-06-24 22:31:58
 * @FilePath     : \data-structure\stack.js
 * @Description  : 栈(后进先出)
 */

class Stack {
  #items; //ES13新特性 在变量前加'#'可以将变量私有化，相当于private 
  constructor(arr) {
    this.#items = arr || []
  }
  // #items=[]
  push(data) { //数据入栈
    this.#items.push(data)
  }
  pop() { //出栈并返回出栈的数据
    return this.#items.pop()
  }
  peek() { //返回栈顶数据
    return this.#items[this.#items.length - 1]
  }
  isEmpty() { //判断栈是否为空
    return this.#items.length === 0
  }
  size() { //返回栈的大小
    return this.#items.length
  }
  clear() { //清空栈
    this.#items = []
  }

}

let s = new Stack()

// 十进转二进制通过栈实现 
// 十进制转任意进制
function convert(decNumber, base) {
  let stack = new Stack([])
  let n = decNumber
  let result = ''
  let baseString = '0123456789ABCDEF'

  while (n > 0) {
    stack.push(n % base)
    n = Math.floor(n / base)
  }

  while(!stack.isEmpty()){
    result += baseString[stack.pop()]
  }

  return result
}
console.log(convert(25,8))